#!/usr/bin/env bash
#
#    Linie - Install Internet Explorer on Linux
#
#    Copyright (C) 2010 Raghu Kaippully <raghu.kb@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


# Logging functions

export LOG_ERROR=0
export LOG_WARNING=1
export LOG_INFO=2
export LOG_DEBUG=3

# Print a message if debug is on
# $* Message to be printed
function log_debug {
	[ "${LOGLEVEL}" -ge "${LOG_DEBUG}" ] && echo "DEBUG: $*"
}

# Print a message if info is on
# $* Message to be printed
function log_info {
	[ "${LOGLEVEL}" -ge "${LOG_INFO}" ] && echo "INFO: $*"
}

# Print a message if warning is on
# $* Message to be printed
function log_warning {
	[ "${LOGLEVEL}" -ge "${LOG_WARNING}" ] && echo "WARNING: $*"
}

# Print a message if error is on
# $* Message to be printed
function log_error {
	[ "${LOGLEVEL}" -ge "${LOG_INFO}" ] && echo "ERROR: $*"
}

# Print all messages from stdin if on debug mode
function debugfilter {
	while read line; do
		log_debug $line
	done
}

# Dependencies

# Check wine installation
function check_wine {
	which wine &> /dev/null || error "It seems wine is not installed"
}

# Check winetricks installation
function check_winetricks {
	which winetricks &> /dev/null || error "It seems winetricks is not installed"
}

# check for wget
function check_wget {
	which wget &> /dev/null || error "It seems wget is not installed"
}

# check for md5sum
function check_md5sum {
	which md5sum &> /dev/null || error "It seems md5sum is not installed"
}

# Download files to a standard location
# $1 is the output filename, $2 is the URL
function download {
	log_info "Downloading $1..."
	(cd ${DOWNLOADDIR}; [ -r "$1" ] || wget --continue --progress=dot:mega --output-document="$1" "$2")
}
